package com.ls.fw.web.test.spring.controller;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;

import java.util.Arrays;

import org.springframework.core.MethodParameter;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.hal.HalLinkDiscoverer;
import org.springframework.hateoas.mvc.ControllerLinkBuilderFactory;
import org.springframework.hateoas.mvc.UriComponentsContributor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.ls.fw.mvc.springmvc.support.hateoas.utils.uri.ControllerUriBuilder;
import com.ls.fw.mvc.springmvc.support.hateoas.utils.uri.ControllerUriResolver;

@RestController
public class GreetingController {

    private static final String TEMPLATE = "Hello, %s!";
   
    ControllerLinkBuilderFactory factory = new ControllerLinkBuilderFactory();

    @RequestMapping("/greeting")
    public HttpEntity<Greeting> greeting(
            @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
    	ControllerUriResolver cr = ControllerUriResolver.on(methodOn(GreetingController.class).greeting(name));
    	Link petsByOwnerLink = ControllerUriBuilder.linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel();
    	System.out.println(petsByOwnerLink);
    	Link link = factory.linkTo(GreetingController.class).withSelfRel();
    	System.out.println(link);
    	String content = "{'_links' :  { 'foo' : { 'href' : '/foo/bar' }}}";
    	LinkDiscoverer discoverer = new HalLinkDiscoverer();
    	Link link1 = discoverer.findLinkWithRel("foo", content);
    	System.out.println(link1.getRel());
    	System.out.println(link1.getHref());
    	 Greeting greeting = new Greeting(String.format(TEMPLATE, name));
        greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());

        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
    }
	@RequestMapping("/sample/{id}")
	public HttpEntity<?> sampleMethod(@PathVariable("id") Long id, SpecialType parameter){
		factory.setUriComponentsContributors(Arrays.asList(new SampleUriComponentsContributor()));
		return null;
	}

	static class SampleUriComponentsContributor implements UriComponentsContributor {

		@Override
		public boolean supportsParameter(MethodParameter parameter) {
			return SpecialType.class.equals(parameter.getParameterType());
		}

		@Override
		public void enhance(UriComponentsBuilder builder, MethodParameter parameter, Object value) {
			builder.queryParam("foo", ((SpecialType) value).parameterValue);
		}
	}

	static class SpecialType {
		String parameterValue;
	}
    
}
